import Cookies from 'js-cookie'

const TokenKey = 'ahxmgk-manage-app'
const path = process.env.VUE_APP_PATH
export function getApp() {
  let app = Cookies.get(TokenKey)
  if(app)return JSON.parse(app)
  return {}
}

export function setApp(token) {
  return Cookies.set(TokenKey, JSON.stringify(token))
}

export function removeApp() {
  return Cookies.remove(TokenKey)
}
export function getQueryVariable(variable)
{
       var query = window.location.search.substring(1);
       var vars = query.split("&");
       for (var i=0;i<vars.length;i++) {
               var pair = vars[i].split("=");
               if(pair[0] == variable){return pair[1];}
       }
       return(false);
}

export function getUrlKey(name) {
  return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ""])[1].replace(/\+/g, '%20')) || null
}
export function redirectUrlCode() {
  let code = getQueryVariable('code')
  let url = path + '/#/?codeq=' + code
  if(window.location.href.indexOf('redbag') >= 0)url += '&redbag=1'
  return url
}
export function getIndexUrl(q) {
  let url = 'http://' + window.location.host + path + '/'
  return url
}
export function getRedbagUrl(q) {
  let url = 'http://' + window.location.host + path + '/#/redbag?redbag=1'
  return url
}
export function getResultUrl(q) {
  let url = 'http://' + window.location.host + path + '/#/result'
  return url
}
//判断是否是微信浏览器的函数
export function isWeiXin() {
  if(process.env.VUE_APP_DEBUG) {
    return true
  }
      //window.navigator.userAgent属性包含了浏览器类型、版本、操作系统类型、浏览器引擎类型等信息，这个属性可以用来判断浏览器类型
      var ua = window.navigator.userAgent.toLowerCase();
      //通过正则表达式匹配ua中是否含有MicroMessenger字符串
      if(ua.match(/MicroMessenger/i) == 'micromessenger'){
        return true;
      }else{
        return false;
      }
}